package valerij_timakov.home_finances.web.costs;

import org.apache.log4j.Logger;

import java.io.Serializable;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import valerij_timakov.home_finances.conf.Conf;
import valerij_timakov.home_finances.data.finances.CostsItem;
import valerij_timakov.home_finances.data.user.UserItem;
import valerij_timakov.home_finances.exceptions.AppExceptionNotAuthorized;
import valerij_timakov.home_finances.exceptions.AppExceptionWrongState;
import valerij_timakov.home_finances.exceptions.AppPersProviderException;
import valerij_timakov.home_finances.logic.AccountWorker;
import valerij_timakov.home_finances.logic.CostsWorker;

@ManagedBean
@SessionScoped
public class CostsBean implements Serializable {
	
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(CostsBean.class);
	
	private Map<Long, CostsItem> costs;
	UserBean userBean;
	private String val = "sdfsf";
	
	public boolean isLoad() throws AppExceptionNotAuthorized, AppPersProviderException, AppExceptionWrongState {
		FacesContext context = FacesContext.getCurrentInstance();
		userBean = (UserBean) context.getApplication().evaluateExpressionGet(context, 
			"#{userBean}", UserBean.class);
		UserItem user = userBean.getUser();
		Conf c = Conf.getInstance();
		try {
			AccountWorker aw = c.getBean(AccountWorker.class);
			long[] accountIds = aw.getUserAccountsIds(userBean.getUser().getId());
			costs = c.getBean(CostsWorker.class).getAccountsCosts(accountIds);
			return true;
		} catch (Exception e) {
			logger.error("Error!", e);
			return false;
		}
	}
	
	public String getVal() {
		return val;
	}

	public void setVal(String val) {
		this.val = val;
	}
	
	
}
